<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>教师列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>

<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!-- 搜索栏 -->
        <fieldset class="table-search-fieldset">
            <legend>教师ID查询</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="teacher-search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">教师ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="teacherId" autocomplete="off" class="layui-input"
                                       placeholder="请输入教师ID（数字）">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</i> 查 询
                            </button>
                            <button type="button" class="layui-btn layui-btn-warm" id="resetBtn">
                                重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!-- 教师信息表格 -->
        <table id="teacherTable" lay-filter="teacherTableFilter"></table>

        <script type="text/html" id="operationBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        // 初始化表格（分页显示全部教师）
        var tableIns = table.render({
            elem: '#teacherTable',
            id: 'teacherTable',
            url: '/teachers/1/10',
            method: 'GET',
            response: {
                statusCode: 200
            },
            parseData: function(res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.records
                };
            },
            cols: [[
                { type: 'checkbox', width: 50, align: 'center' },
                { field: 'teacherId', title: '教师ID', width: 100, align: 'center', sort: true },
                { field: 'teacherName', title: '姓名', width: 120, align: 'center' },
                { field: 'institute', title: '学院', width: 180, align: 'center' },
                { field: 'sex', title: '性别', width: 80, align: 'center' },
                { field: 'tel', title: '电话号码', width: 150, align: 'center' },
                { field: 'email', title: '电子邮件', width: 200, align: 'center' },
                { field: 'cardId', title: '身份证号', width: 220, align: 'center' },
                { field: 'type', title: '职称', width: 120, align: 'center' },
                {
                    field: 'role', title: '角色', width: 100, align: 'center',
                    templet: function (d) {
                        return d.role === '0' ? '管理员' : d.role === '1' ? '教师' : '学生';
                    }
                },
                { title: '操作', width: 180, align: 'center', toolbar: '#operationBar' }
            ]],
            page: {
                limit: 10,
                limits: [10, 20, 50]
            },
            skin: 'line',
            even: true
        });

        // 教师ID精确查询
        form.on('submit(data-search-btn)', function (data) {
            var teacherId = data.field.teacherId.trim();
            if (!teacherId) {
                layer.msg('教师ID不能为空', { icon: 2 });
                return false;
            }
            if (isNaN(Number(teacherId))) {
                layer.msg('教师ID必须是数字', { icon: 2 });
                return false;
            }

            $.ajax({
                url: '/teacher/' + teacherId,
                type: 'GET',
                dataType: 'json',
                success: function (res) {
                    if (res.code === 200) {
                        if (res.data) {
                            tableIns.reload({
                                data: [res.data],
                                page: false,
                                url: null
                            });
                            layer.msg('查询到1条数据', { icon: 1 });
                        } else {
                            tableIns.reload({ data: [] });
                            layer.msg('未找到教师ID为[' + teacherId + ']的信息', { icon: 2 });
                        }
                    } else {
                        layer.msg('查询失败：' + (res.msg || '未知错误'), { icon: 2 });
                    }
                },
                error: function () {
                    layer.msg('服务器异常，请重试', { icon: 2 });
                }
            });

            return false;
        });

        // 重置按钮
        $('#resetBtn').on('click', function () {
            $('input[name="teacherId"]').val('');
            tableIns.reload({
                url: '/teachers/1/10',
                page: { curr: 1 },
                where: {}
            });
        });

        // 编辑/删除操作
        table.on('tool(teacherTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                // 把当前教师数据存入 localStorage
                localStorage.setItem('currentTeacher', JSON.stringify(data));
                layer.open({
                    title: '编辑教师信息',
                    type: 2,
                    shade: 0.3,
                    area: ['80%', '80%'],
                    content: 'teacherEdit.html'
                });
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除教师ID为[' + data.teacherId + ']的教师？', {
                    icon: 3,
                    title: '删除确认'
                }, function (index) {
                    $.ajax({
                        url: '/teacher/' + data.teacherId,
                        type: 'DELETE',
                        dataType: 'json',
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg('删除成功', { icon: 1 });
                                obj.del();
                            } else {
                                layer.msg('删除失败：' + (res.msg || '未知错误'), { icon: 2 });
                            }
                        },
                        error: function () {
                            layer.msg('服务器异常，请重试', { icon: 2 });
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>
</html>
